<?php
/*
 * 行末注释一般为功能注释，代码注释一般为测试请勿删除
 * */
class staffManageMod extends commonMod
{
	public function __construct(){
		parent::__construct();
	}

	public function index(){
		if(!empty($_GET['rollPage'])){
			$rollPage  = intval($_GET['rollPage']);			
		}else{
			$rollPage  = 1;
		}
		$totalRows = $this->model->table('staff_detail')->count();
		$startFrom  = ($rollPage-1)*30;
		$page = parent::page('index', $totalRows, 30, $rollPage,2);
		$staffInfo = $this->model->table('staff_detail')->field('id,uid,name,department,tel1,mobile,qq')->limit("$startFrom,30")->select();
		//echo $this->model->getSql();		
		$key = 0;
		foreach ( $staffInfo as $vo ){
			//dump($vo);
			$status           = $this->model->table('admin')->field('id,isLock')->where("`id`=".$vo['uid'])->find();
			$status['isLock'] = ($status['isLock']!=1)?'激活':'锁定';
			$rightCat         = parent::getRightCat($vo['department']);
			$rightCat 		  = array_merge($rightCat, $status);
			$staffInfo[$key]  = array_merge($staffInfo[$key], $rightCat);
			$key++;
		}
		//dump($staffInfo);
		//dump($page);
		$this->assign('page', $page);
		$this->assign('count', $totalRows);
		$this->assign('staffInfo', $staffInfo);
		$this->display();
	}
	//添加员工
	public function add(){
		if(!empty($_POST['username']) && !empty($_POST['password']) && ($_POST['isLock']!='')){
			$data['username'] = in($_POST['username']);
			$data['password'] = md5(in($_POST['password']));
			$data['isLock']   = $_POST['isLock'];
		}else{
			echo $msg = '请按提示填写用户名密码和状态';
			exit();
		}
		$condition = array('username'=>$data['username']);
		if(parent::checkUniqu($condition,'admin')){
			echo $msg = '用户名已经存在';
		}else{
			$result = $this->model->table('admin')->data($data)->insert();
			if($result){
				echo $msg = '添加成功';
			}else{
				echo $msg = '添加失败';
			}
		}
	}
	
	//添加附件
	public function addAcce(){
		if(!empty($_POST['userId'])){
			$uid = intval($_POST['userId']);
		}else{
			$this->error('关键参数丢失!');
		}
		$acceInfo = $this->model->table('accessory')->where('`uid`='.$uid)->count();
		if($acceInfo>=5){
			$this->error('附件不能超过5个！');
		}
		unset($acceInfo);
		$data['uid'] = $uid;
		$acceInfo = $this->_upload('menbersPhoto');//设置上传路径
		//dump($acceInfo);
		$dbUrl = '/upload/menbersPhoto/';
		$data['accessoryPath'] = $dbUrl.$acceInfo[0]['savename'];//组装存入数据库的路径
		$result = $this->model->table('accessory')->data($data)->insert();
		if($result){
			$this->success('添加成功！');
		}else{
			$this->error('添加失败！');
		}
	}
	
	//修改员工信息
	public function edit(){
		if(!empty($_GET['uid'])){
			$uid = intval($_GET['uid']);
		}else{
			$this->error('关键参数丢失！请返回重试!如仍未解决请联系网络部！');
		}
		$result = $this->model->table('staff_detail')->where("`uid`=$uid")->find();
		if($result){
			$result['birthday'] = date('Y-m-d',$result['birthday']);
			$result['ruzhitime'] = date('Y-m-d',$result['ruzhitime']);
			$result['lizhitime'] = date('Y-m-d',$result['lizhitime']);
			$this->assign('cat',$this->getCat());
			//dump($result);
			$this->assign('result', $result);
			$this->display();
		}else{
			$this->error('数据查询失败！');
		}
		
	}
	
	//更新员工信息
	public function updateStaff(){
		$uid                      = intval($_POST['uid']);
		$pdata['name']            = in($_POST['username']);//$pdata means personalData
		$pdata['sex']             = in($_POST['sex']);
		$pdata['isMarry']         = in($_POST['isMarry']);
		$pdata['birthday']        = exchangeDate($_POST['birthday']);		
		$pdata['department']      = in($_POST['department']);
		$pdata['xueli']           = in($_POST['xueli']);
		$pdata['shenfenzhengNum'] = in($_POST['shenfenzhengNum']);
		$pdata['hukoulocation']   = in($_POST['hukoulocation']);
		$pdata['addr']            = in($_POST['addr']);
		$pdata['ruzhitime']       = exchangeDate($_POST['ruzhitime']);
		$pdata['lizhitime']       = exchangeDate($_POST['lizhitime']);
		$pdata['salary']          = in($_POST['salary']);
		$pdata['tel1']            = in($_POST['tel1']);
		$pdata['tel2']            = in($_POST['tel2']);
		$pdata['mobile']          = in($_POST['mobile']);
		$pdata['qq']              = in($_POST['qq']);
		$pdata['msn']             = in($_POST['msn']);
		$pdata['email']           = in($_POST['email']);
		$pdata['jianli']          = in($_POST['jianli']);
		$pdata['family']          = in($_POST['family']);
		//dump($pdata);
		//echo $uid.'<br />';
		$result = $this->model->table('staff_detail')->data($pdata)->where('`uid` ='.$uid)->update();
		//echo $this->model->getSql();
		if($result){
			$this->success('操作成功！');
		}else{
			$this->error('操作失败！');
		}
	}
	
	//附件管理
	public function picEdit(){
		if(isset($_GET['uid'])){
			$uid = intval($_GET['uid']);
		}else{
			$this->error('关键参数丢失！');
		}
		$staffPic     = $this->model->table('staff_detail')->field('uid,name,photo')->where('`uid`='.$uid)->find();
		$accessoryPic = $this->model->table('accessory')->where('`uid`='.$uid)->select();
		$this->assign('staffPic', $staffPic);
		$this->assign('accessoryPic', $accessoryPic);
		$this->display();
	}
	
	//照片及附件删除
	public function delPic(){
		if(isset($_POST['id'])){
			$id = intval($_POST['id']);
		}else{
			echo $msg = '关键参数丢失！';
		}
		$result = $this->model->table('accessory')->where('`id`='.$id)->delete();
		if($result){
			echo $msg = '删除成功！';
		}else{
			echo $msg = '删除失败！';
		}
	}
	
	//删除员工
	public function delALL(){
		if(!empty($_POST['staffId'])){
			$data = $_POST['staffId'];
		}else{
			$this->error('请选择要删除的员工！'); 
		}		
		foreach( $data as $vo){
			$condition .= intval($vo).','; //组装删除条件
		}
		//dump($data);
		$condition = trim($condition, ',');//去除最后的逗号
		//echo $condition;
		$adminInfo = $this->model->table('admin')->where("`id` in (".$condition.")")->delete();
		//echo $this->model->getSql();
		if($adminInfo){
			$staff_detailInfo = $this->model->table('staff_detail')->where("`uid` in (".$condition.")")->delete();
			$accessoryInfo = $this->model->table('accessory')->where("`uid` in (".$condition.")")->delete();
			if ($staff_detailInfo && $accessoryInfo){
				$this->success('删除成功');
			}else{
				$this->error('登录帐号删除成功，个人信息或者附件删除失败！');
			}
		}else{
			$this->error('删除失败，请返回重试，如仍未能解决，请联系网络部！');//用户不存在也会出现删除失败
		}
	}
}
?>